package ru.housewife.service.regions;

import org.hibernate.Query;
import org.hibernate.Session;
import ru.common.hibernate.HibernateAction;
import ru.housewife.form.RegionForm;
import ru.housewife.model.Region;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: add description
 *
 * @author Alexander Troshanin
 */
public class GetAllRegions implements HibernateAction<List<RegionForm>> {
    @Override
    public List<RegionForm> execute(Session session) {
        Query q = session.createQuery("select r from Region r where r.parent is NULL");
        List<Region> lst = q.list();
        List<RegionForm> result = new ArrayList<RegionForm>(lst.size());
        for (Region r : lst) {
            result.add(new RegionForm(r, null));
        }
        return result;
    }
}
